home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / DroneZone / DZSound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  7.8 KB  |  302 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        DZSound.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Contents:    Handles some of the sound stuff.
  6.  *
  7.  *    Copyright © 1996 Apple Computer, Inc.
  8.  */
  9.  
  10. #include <assert.h>
  11. #include <math.h>
  12.  
  13. #include <Controls.h>
  14. #include <Dialogs.h>
  15. #include <Sound.h>
  16.  
  17. #include "SoundSprocket.h"
  18.  
  19. #include "DZResource.h"
  20. #include "DZSound.h"
  21.  
  22. static ListenerReference    gSoundListener            = NULL;
  23. static UserItemUPP            gSoundBoxUserItemProc    = NULL;
  24. static UserItemUPP            gSoundOKUserItemProc    = NULL;
  25.  
  26.  
  27. static pascal void Sound_BoxUserItem(
  28.     DialogPtr            inDialog,
  29.     short                inItem);
  30.  
  31. static pascal void Sound_OKUserItem(
  32.     DialogPtr            inDialog,
  33.     short                inItem);
  34.  
  35.  
  36. /* =============================================================================
  37.  *        Sound_Init (external)
  38.  *
  39.  *    Initializes the sound stuff.
  40.  * ========================================================================== */
  41. void Sound_Init(
  42.     void)
  43. {
  44.     NewListener(&gSoundListener);
  45.     assert(gSoundListener != NULL);
  46.     
  47.     SetListenerMetersPerUnit(gSoundListener, 5.0);
  48.     
  49.     gSoundBoxUserItemProc = NewUserItemProc(Sound_BoxUserItem);
  50.     assert(gSoundBoxUserItemProc != NULL);
  51.     
  52.     gSoundOKUserItemProc = NewUserItemProc(Sound_OKUserItem);
  53.     assert(gSoundOKUserItemProc != NULL);
  54. }
  55.  
  56.  
  57. /* =============================================================================
  58.  *        Sound_Exit (external)
  59.  *
  60.  *    Prepares for exit.
  61.  * ========================================================================== */
  62. void Sound_Exit(
  63.     void)
  64. {
  65.     if (gSoundListener != NULL)
  66.     {
  67.         DisposeListener(gSoundListener);
  68.         gSoundListener = NULL;
  69.     }
  70.     
  71.     if (gSoundBoxUserItemProc != NULL)
  72.     {
  73.         DisposeRoutineDescriptor(gSoundBoxUserItemProc);
  74.         gSoundBoxUserItemProc = NULL;
  75.     }
  76.     
  77.     if (gSoundOKUserItemProc != NULL)
  78.     {
  79.         DisposeRoutineDescriptor(gSoundOKUserItemProc);
  80.         gSoundOKUserItemProc = NULL;
  81.     }
  82. }
  83.  
  84.  
  85. /* =============================================================================
  86.  *        Sound_GetListener (external)
  87.  *
  88.  *    Returns the game listener object, which is the "head" position for 3D sound.
  89.  * ========================================================================== */
  90. ListenerReference Sound_GetListener(
  91.     void)
  92. {
  93.     assert(gSoundListener != NULL);
  94.     return gSoundListener;
  95. }
  96.  
  97.  
  98. /* =============================================================================
  99.  *        Sound_Configure (external)
  100.  *
  101.  *    Presents the modal dialog to configure the 3D sound stuff.  Because the
  102.  *    state of the 3D sound speaker kind and angle is global to all localized
  103.  *    sound channels, we make a fresh channel for accessing the parameters.
  104.  * ========================================================================== */
  105. void Sound_Configure(
  106.     void)
  107. {
  108.     //• TODO: Command-period should cancel
  109.     
  110.     DialogPtr            dialog;
  111.     short                itemType;
  112.     Handle                itemHandle;
  113.     Rect                itemBounds;
  114.     ControlHandle        stereoControl;
  115.     ControlHandle        monoControl;
  116.     ControlHandle        headphonesControl;
  117.     ControlHandle        angleControl;
  118.     SndChannelPtr        sndChannel;
  119.     Snd3DSetup            snd3DInitialSetup;
  120.     Snd3DSetup            snd3DSetup;
  121.     SoundComponentLink    link;
  122.     Boolean                changed;
  123.     Boolean                ok;
  124.     short                item;
  125.     
  126.     // Grab the dialog
  127.     dialog = GetNewDialog(kDlogID_Config3DSound, NULL, (WindowPtr) -1);
  128.     assert(dialog != NULL);
  129.     
  130.     // Grab the control handles
  131.     GetDialogItem(dialog, kConfig3DSoundItem_Stereo, &itemType, &itemHandle, &itemBounds);
  132.     stereoControl = (ControlHandle) itemHandle;
  133.     
  134.     GetDialogItem(dialog, kConfig3DSoundItem_Mono, &itemType, &itemHandle, &itemBounds);
  135.     monoControl = (ControlHandle) itemHandle;
  136.     
  137.     GetDialogItem(dialog, kConfig3DSoundItem_Headphones, &itemType, &itemHandle, &itemBounds);
  138.     headphonesControl = (ControlHandle) itemHandle;
  139.     
  140.     GetDialogItem(dialog, kConfig3DSoundItem_Angle, &itemType, &itemHandle, &itemBounds);
  141.     angleControl = (ControlHandle) itemHandle;
  142.     
  143.     // Do the user items
  144.     GetDialogItem(dialog, kConfig3DSoundItem_SetupBox, &itemType, &itemHandle, &itemBounds);
  145.     SetDialogItem(dialog, kConfig3DSoundItem_SetupBox, itemType, (Handle) gSoundBoxUserItemProc, &itemBounds);
  146.     
  147.     GetDialogItem(dialog, kConfig3DSoundItem_AngleBox, &itemType, &itemHandle, &itemBounds);
  148.     SetDialogItem(dialog, kConfig3DSoundItem_AngleBox, itemType, (Handle) gSoundBoxUserItemProc, &itemBounds);
  149.     
  150.     GetDialogItem(dialog, kConfig3DSoundItem_OKHilite, &itemType, &itemHandle, &itemBounds);
  151.     SetDialogItem(dialog, kConfig3DSoundItem_OKHilite, itemType, (Handle) gSoundOKUserItemProc, &itemBounds);
  152.     
  153.     // Set up a sound channel to control the speaker kind and angle
  154.     sndChannel = NULL;
  155.     SndNewChannel(&sndChannel, sampledSynth, initMono, NULL);
  156.     assert(sndChannel != NULL);
  157.     
  158.     // Install the 3D sound filters
  159.     link.description.componentType            = kSoundEffectsType;
  160.     link.description.componentSubType        = kSnd3DSubType;
  161.     link.description.componentManufacturer    = 'appl';
  162.     link.description.componentFlags            = 0;        
  163.     link.description.componentFlagsMask        = 0;    
  164.     link.mixerID                            = nil;
  165.     link.linkID                                = nil;
  166.     
  167.     SndSetInfo(sndChannel, siPreMixerSoundComponent, &link);
  168.     
  169.     // Get the initial state of the setup
  170.     SndGetInfo(sndChannel, si3DSetup, &snd3DSetup);
  171.     snd3DInitialSetup = snd3DSetup;
  172.     
  173.     // Process events
  174.     changed = true;
  175.     ok = true;
  176.     do
  177.     {
  178.         // Update the dialog
  179.         if (changed)
  180.         {
  181.             SetControlValue(stereoControl,        snd3DSetup.speakerKind == kSpeakerKindStereo);
  182.             SetControlValue(monoControl,        snd3DSetup.speakerKind == kSpeakerKindMono);
  183.             SetControlValue(headphonesControl,    snd3DSetup.speakerKind == kSpeakerKindHeadphones);
  184.             SetControlValue(angleControl,        snd3DSetup.speakerAngle * 180.0 / _PI);
  185.             
  186.             HiliteControl(angleControl, (snd3DSetup.speakerKind == kSpeakerKindStereo) ? 0 : 255);
  187.             
  188.             changed = false;
  189.         }
  190.         
  191.         // Grab the next dialog event
  192.         ModalDialog(NULL, &item);
  193.         
  194.         // Process the dialog item
  195.         switch (item)
  196.         {
  197.             case kConfig3DSoundItem_OK:
  198.                 ok = false;
  199.             break;
  200.             
  201.             case kConfig3DSoundItem_Cancel:
  202.                 snd3DSetup = snd3DInitialSetup;
  203.                 changed = true;
  204.                 ok = false;
  205.             break;
  206.             
  207.             case kConfig3DSoundItem_Stereo:
  208.                 snd3DSetup.speakerKind = kSpeakerKindStereo;
  209.                 changed = true;
  210.             break;
  211.             
  212.             case kConfig3DSoundItem_Mono:
  213.                 snd3DSetup.speakerKind = kSpeakerKindMono;
  214.                 changed = true;
  215.             break;
  216.             
  217.             case kConfig3DSoundItem_Headphones:
  218.                 snd3DSetup.speakerKind = kSpeakerKindHeadphones;
  219.                 changed = true;
  220.             break;
  221.             
  222.             case kConfig3DSoundItem_Angle:
  223.                 snd3DSetup.speakerAngle = GetControlValue(angleControl) * _PI / 180.0;
  224.                 changed = true;
  225.             break;
  226.             
  227.             default:
  228.                 assert(0);
  229.         }
  230.         
  231.         // Update the sound channel
  232.         if (changed)
  233.         {
  234.             SndSetInfo(sndChannel, si3DSetup, &snd3DSetup);
  235.         }
  236.     }
  237.     while (ok);
  238.     
  239.     // Get rid of our channel
  240.     SndDisposeChannel(sndChannel, true);
  241.     sndChannel = NULL;
  242.     
  243.     // Take down the dialog
  244.     DisposDialog(dialog);
  245. }
  246.  
  247.  
  248. /* =============================================================================
  249.  *        Sound_BoxUserItem (internal)
  250.  *
  251.  *    Draws the user item by framing it with a gray box.
  252.  * ========================================================================== */
  253. pascal void Sound_BoxUserItem(
  254.     DialogPtr            inDialog,
  255.     short                inItem)
  256. {
  257.     short                itemType;
  258.     Handle                itemHandle;
  259.     Rect                itemBounds;
  260.     RGBColor            color;
  261.     
  262.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  263.     
  264.     color.red   =
  265.     color.green =
  266.     color.blue  = 0x7777;
  267.     
  268.     RGBForeColor(&color);
  269.     
  270.     FrameRect(&itemBounds);
  271.     
  272.     color.red   =
  273.     color.green =
  274.     color.blue  = 0x0000;
  275.     
  276.     RGBForeColor(&color);
  277. }
  278.  
  279.  
  280. /* =============================================================================
  281.  *        Sound_OKUserItem (internal)
  282.  *
  283.  *    Draws the user item used for framing the OK button.  The user item should
  284.  *    be four pixels larger than the OK button in each direction.
  285.  * ========================================================================== */
  286. pascal void Sound_OKUserItem(
  287.     DialogPtr            inDialog,
  288.     short                inItem)
  289. {
  290.     short                itemType;
  291.     Handle                itemHandle;
  292.     Rect                itemBounds;
  293.     
  294.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  295.     
  296.     PenSize(3, 3);
  297.     FrameRoundRect(&itemBounds, 16, 16);
  298.     PenSize(1, 1);
  299. }
  300.  
  301.  
  302.